<?php
declare (strict_types=1);

namespace app\common\listener;

use app\common\cache\TheRedis;
use app\common\model\Member;
use app\common\model\MemberRule;
use app\common\token\Token;
use think\facade\Request;

/**
 * 前台权限校验监听
 * Class MemberCheckRight
 * @package app\listener
 */
class IndexCheckRight
{
    /**
     * 事件监听处理
     * @return mixed
     */
    public function handle($admin_id)
    {
        $module = strtolower(app('http')->getName());
        $controller = strtolower(Request::controller());
        $action = Request::action();
        $admin_info = Token::instance()->getUserInfo();

        $rule_list = MemberRule::instance()->getUrlRuleList($module, $controller, $action);
        $user_rule_list = (Member::instance())->getRuleList($admin_info);
        $user_rule_id_arr = $user_rule_list->column(MemberRule::instance()->getPk());
        $url_rule_id_arr = $rule_list->column(MemberRule::instance()->getPk());

        if (array_intersect($url_rule_id_arr, $user_rule_id_arr)) {
            return true;
        }
        ExceptionEmun::throwException(UserException::NO_RIGHT);
        return false;
    }
}
